package cn.hgy.domain.activity.service.trial.node;

import cn.hgy.domain.activity.model.entity.MarketProductEntity;
import cn.hgy.domain.activity.model.entity.TrialBalanceEntity;
import cn.hgy.domain.activity.service.trial.AbstractGroupBuyMarketSupport;
import cn.hgy.domain.activity.service.trial.factory.DefaultActivityStrategyFactory;
import cn.hgy.types.enums.ResponseCode;
import cn.hgy.types.exception.AppException;
import cn.hgy.types.tree.StrategyHandler;
import com.alibaba.fastjson2.JSON;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * 拼团商品查询试算核心服务类，继承自活动策略抽象基类
 * 采用策略模式实现对不同营销活动的商品试算路由
 * 注：当前类已禁用多线程处理（multiThread方法为空）
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class RootNode
        extends AbstractGroupBuyMarketSupport<MarketProductEntity, DefaultActivityStrategyFactory.DynamicContext, TrialBalanceEntity> {

    private final SwitchRoot switchRoot;

    /**
     * 多线程处理占位方法（当前未实现具体功能）
     * @param requestParameter 商品请求参数
     * @param dynamicContext 策略动态上下文
     */
    @Override
    protected void multiThread(MarketProductEntity requestParameter, DefaultActivityStrategyFactory.DynamicContext dynamicContext) {
    }

    /**
     * 核心试算逻辑执行方法
     * @param requestParameter 商品请求实体（包含用户/商品信息）
     * @param dynamicContext 策略运行时上下文
     * @return 试算结果实体
     * @throws Exception 处理异常时抛出
     */
    @Override
    protected TrialBalanceEntity doApply(MarketProductEntity requestParameter, DefaultActivityStrategyFactory.DynamicContext dynamicContext) throws Exception {

        log.info("拼团商品查询试算服务-RootNode userId:{} requestParameter:{}", requestParameter.getUserId(), JSON.toJSONString(requestParameter));

        // 参数判断
        if (StringUtils.isBlank(requestParameter.getUserId()) ||
                StringUtils.isBlank(requestParameter.getGoodsId()) ||
                StringUtils.isBlank(requestParameter.getSource()) ||
                StringUtils.isBlank(requestParameter.getChannel())) {
            throw new AppException(ResponseCode.ILLEGAL_PARAMETER.getCode(), ResponseCode.ILLEGAL_PARAMETER.getInfo());
        }

        // 路由至具体的策略处理器执行试算逻辑
        return router(requestParameter, dynamicContext);
    }

    /**
     * 获取当前请求对应的策略处理器
     * @param requestParameter 商品请求参数
     * @param dynamicContext 策略动态上下文
     * @return 策略处理器实例
     * @throws Exception 路由选择异常
     */
    @Override
    public StrategyHandler<MarketProductEntity, DefaultActivityStrategyFactory.DynamicContext, TrialBalanceEntity> get(MarketProductEntity requestParameter, DefaultActivityStrategyFactory.DynamicContext dynamicContext) throws Exception {
        return switchRoot;
    }
}
